package com.lifesea.notify.service;

import com.alibaba.fastjson.JSONObject;
import com.lifesea.commons.exception.ErrorCodeConst;
import com.lifesea.commons.exception.ErrorCodeException;
import com.lifesea.commons.utils.HttpUtils;
import com.lifesea.conifg.PublicConfig;
import com.lifesea.model.dto.AppPushRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class EHPushService implements IPushService {

    private final Logger logger = LoggerFactory.getLogger(EHPushService.class);

    @Override
    public void push(AppPushRequest request) {
        Map<String, String> params = new HashMap<>();
        params.put("method", "lifesea.push.msg.send");

        params.put("did", String.valueOf(request.getDid()));
        params.put("ecn", String.valueOf(request.getEcn()));
        params.put("body", request.getContent());
        //params.put("ref_type", request.getBusinessId());
        params.put("ref_id", request.getBusinessId());
        params.put("category", "601");

        String rest = null;

        rest = HttpUtils.doPost(PublicConfig.PUSH_URL, params);

        logger.info("request={}, response={}", params, rest);

        if (rest != null) {
            JSONObject json = JSONObject.parseObject(rest);
            if (0 != json.getInteger("code")) {
                logger.error("向APP推送消息失败,result={}", json);
                throw new ErrorCodeException(ErrorCodeConst.GLOBAL_RESPONSE_ERROR, "向APP推送消息失败");
            }
        }
    }

}

